package com.andnnl.hutool;

import cn.hutool.core.util.ReUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;


/**
 * Created by chenss on 2020/12/25.
 */
public class RegexTest {
    public static void main(String[] args) throws InterruptedException, ExecutionException, IOException {
//        test1();
//
//
//        test2();
//        test3();

        testReplace();
        testReplaceHutool();
    }

    private static void testReplaceHutool() {
        String subjectString = "asdfk$asdf$lasd$3sdf$flk1$d3sx$2409werfjaskldflk";
        Pattern regex = Pattern.compile("\\$(\\w+)\\$");
        String rs=ReUtil.replaceAll(subjectString, regex, (s) -> {
            System.out.println(s.group(1));
            System.out.println(s.group(0));
            return "["+s.group(1)+"]";
        });
        System.out.println(rs);
    }

    private static void testReplace() {
        String subjectString = "asdfk$asdf$lasd$3sdf$flk1$d3sx$2409werfjaskldflk";
        try {
            Pattern regex = Pattern.compile("\\$(\\w+)\\$");
            Matcher regexMatcher = regex.matcher(subjectString);
            StringBuffer sb = new StringBuffer();
            while (regexMatcher.find()) {
//                System.out.println(regexMatcher.group());
                String paramName = regexMatcher.group(1);
                System.out.println(paramName);
                regexMatcher.appendReplacement(sb, "___");

            }
            regexMatcher.appendTail(sb);

            System.out.println(sb);
        } catch (PatternSyntaxException ex) {
            // Syntax error in the regular expression
        }

    }

    private static void test1() {
        String s = "setVfyTkn('DrHj0bhyY1tiItcvLubXg4E+ypuOYMTOZSR5Z5wFRHvxIUMmRFcqAQ==');";
//        String s="setVfyTkn('DrHj0bhyY1tiItcvLubXg4E+ypuOYMTOZSR5Z5wFRHvxIUMmRFcqAQ==22')";
        String s1 = ReUtil.getGroup1("setVfyTkn\\('(.+)'\\);", s);
        System.out.println(s1);

        try {
            Pattern regex = Pattern.compile("setVfyTkn\\('(.+)'\\);", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.MULTILINE);
            Matcher regexMatcher = regex.matcher(s);
            while (regexMatcher.find()) {
                for (int i = 1; i <= regexMatcher.groupCount(); i++) {
                    System.out.println(regexMatcher.group(i));
                    // matched text: regexMatcher.group(i)
                    // match start: regexMatcher.start(i)
                    // match end: regexMatcher.end(i)
                }
            }
        } catch (PatternSyntaxException ex) {
            // Syntax error in the regular expression
        }
    }

    private static void test3() {
        Pattern regex = Pattern.compile("(http://|https://)?([\\w-~]+\\.)+([a-z]){2,6}(\\/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|])?", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.MULTILINE);

        String subjectString = "6212?1-2:a=a11&b=123123|1-2:a=a11&b=苛夺苛夺苛夺工顶戴顶戴塔顶地&c=苛夺苛夺苛夺工顶戴顶戴塔顶地1\n" +
                "http://adsf.clsdf.df/asdfl3d ,http://adsf.clsdf.df/asdfl3d333\n" +
                "e";

        System.out.println(ReUtil.findAllGroup0(regex, subjectString));
    }

    private static void test2() {
        List<String> matchList = new ArrayList<String>();
        try {
            Pattern regex = Pattern.compile("(http://|https://)?([\\w-~]+\\.)+([a-z]){2,6}(\\/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|])?", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.MULTILINE);
            String subjectString = "6212?1-2:a=a11&b=123123|1-2:a=a11&b=苛夺苛夺苛夺工顶戴顶戴塔顶地&c=苛夺苛夺苛夺工顶戴顶戴塔顶地1\n" +
                    "http://adsf.clsdf.df/asdfl3d ,http://adsf.clsdf.df/asdfl3d333\n" +
                    "e";
            Matcher regexMatcher = regex.matcher(subjectString);
            while (regexMatcher.find()) {
                matchList.add(regexMatcher.group());
            }
        } catch (PatternSyntaxException ex) {
            // Syntax error in the regular expression
        }
        System.out.println(matchList);

    }
}
